package netty.person;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import netty.protobuf.DataInfo;
import netty.serviceclient.MyClientInitializer;


public class MyClient {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup eventExecutors = new NioEventLoopGroup();

        try {

            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventExecutors).channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline pipeline = ch.pipeline();
                            //解码时，解决粘包/半包的问题
                            pipeline.addLast(new ProtobufVarint32FrameDecoder());
                            //对接收到的信息，使用protobuf进行解码
                            pipeline.addLast(new ProtobufDecoder(MyDataInfo.Person.getDefaultInstance()));
                            //编码时，解决粘包/半包的问题
                            pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
                            //对于要发送的信息，使用protobuf进行编码
                            pipeline.addLast(new ProtobufEncoder());
                            pipeline.addLast(new SimpleChannelInboundHandler<MyDataInfo.Person>() {
                                @Override
                                protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.Person msg) throws Exception {
                                    System.out.println(msg.getName());
                                    System.out.println(msg.getAge());
                                    System.out.println(msg.getAddress());
                                }

                                @Override
                                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                    super.channelActive(ctx);
                                    MyDataInfo.Person person = MyDataInfo.Person.newBuilder().
                                            setName("张三").setAge(23).setAddress("北京").build();
                                    //给服务端发送数据
                                    ctx.channel().writeAndFlush(person);
                                }
                            });


                        }
                    });;
            ChannelFuture channelFuture = bootstrap.connect("localhost", 8899).sync();

            channelFuture.channel().closeFuture().sync();
        }finally {
            eventExecutors.shutdownGracefully();
        }

    }
}
